package cn.org.timespace.ons.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;
import cn.org.timespace.ons.service.base.MachineTypeNameValueService;
import cn.org.timespace.ons.service.base.MachinesApplyService;
import cn.org.timespace.ons.service.base.MachinesConfService;
import cn.org.timespace.ons.service.base.MachinesService;
import cn.org.timespace.ons.service.base.UserMachineTypeService;
import cn.org.timespace.ons.util.ExcelFileFilter;
import cn.org.timespace.ons.util.ResultUtil;
import cn.org.timespace.ons.util.datatime.DateTimeUtils;
import cn.org.timespace.ons.util.json.JSONObject;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.util.pager.PagerHelper;
import cn.org.timespace.ons.vo.base.AppleServer;
import cn.org.timespace.ons.vo.base.MachineTypeNameValue;
import cn.org.timespace.ons.vo.base.Machines;
import cn.org.timespace.ons.vo.base.MachinesApply;
import cn.org.timespace.ons.vo.base.MachinesConf;
import cn.org.timespace.ons.vo.base.Options;
import cn.org.timespace.ons.vo.base.UserMachineType;

import com.opensymphony.xwork2.ActionContext;

import cn.org.timespace.ons.util.json.JSONResponseHepler;

public class MachinesAction extends EFBaseAction {

	private static Log log = LogFactory.getLog(MachinesAction.class);
	private MachinesService machinesService;
	private MachinesConfService machinesConfService;
	private MachinesApplyService machinesApplyService; 
	private MachineTypeNameValueService machineTypeNameValueService;
	private UserMachineTypeService userMachineTypeService;
	
	private File fileUpload;
	
	
	public File getFileUpload() {
		return fileUpload;
	}
	public void setFileUpload(File fileUpload) {
		this.fileUpload = fileUpload;
	}
	
	public UserMachineTypeService getUserMachineTypeService() {
		return userMachineTypeService;
	}
	public void setUserMachineTypeService(
			UserMachineTypeService userMachineTypeService) {
		this.userMachineTypeService = userMachineTypeService;
	}
	
	public MachineTypeNameValueService getMachineTypeNameValueService() {
		return machineTypeNameValueService;
	}
	public void setMachineTypeNameValueService(
			MachineTypeNameValueService machineTypeNameValueService) {
		this.machineTypeNameValueService = machineTypeNameValueService;
	}
	
	public MachinesApplyService getMachinesApplyService() {
		return machinesApplyService;
	}
	public void setMachinesApplyService(MachinesApplyService machinesApplyService) {
		this.machinesApplyService = machinesApplyService;
	}
	public MachinesConfService getMachinesConfService() {
		return machinesConfService;
	}
	public void setMachinesConfService(MachinesConfService machinesConfService) {
		this.machinesConfService = machinesConfService;
	}
	public MachinesService getMachinesService() {
		return machinesService;
	}
	public void setMachinesService(MachinesService machinesService) {
		this.machinesService = machinesService;
	}
	
	public MachinesAction()
	{
		super(new MachinesModel());
		
	}
	
	MachinesModel machinesModel = (MachinesModel) this.getModel();
	
	
	/**
	 * 显示终端信息列表
	 * @return
	 * @throws Exception
	 */
	public String doList() throws Exception{
		
		machinesModel.setUserName(getUserAccount());
		if(machinesModel.getCompCode()==null){
			machinesModel.setStatus(1);
		}
		Pager p = PagerHelper.getPagerInstance(getRequest());
		List<Object> resList = machinesService.findByCondition(machinesModel,p);
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
		List list = doListMachineTypeName();
		Map request = (Map)ActionContext.getContext().get("request");
		request.put("list",list);
		return LIST;
	}
	
	/**
	 * 保存终端信息
	 * @return
	 * @throws Exception
	 */
	public String doSave() throws Exception{
		Machines machines = machinesModel.getMachines();
		String theIdMachine = machines.getTheIdMachine();
		UserMachineType userMachineType = userMachineTypeService.findById(theIdMachine);
		String mid = IdentityGenerator.getInstance().getmacId();
		machines.setMid(mid);
		machines.setUserName(getUserAccount());
		
		//组合键ID
		String machinesID = machines.getCompCode()+
							machines.getDeviceCode()+machines.getDeviceRandom();
		machines.setMachinesID(machinesID);
		
		//type暂时不使用，随便保存一个默认值
		machines.setType("0000000000000000");
		//保存终端信息
		//增加地区码
//		String sheng =	machines.getSelA();//省
//	    String shi =	machines.getSelB();//市
//	    String qu = machines.getSelC();//县/区
//	    String id = "";
//	    if(qu!=null&&qu.length()>0){
//	    	 id = qu;
//	    }else if(shi!=null&&shi.length()>0){
//	    	
//	    	 id = shi;	
//	    }else if(sheng!=null&&sheng.length()>0){
//	    	 id = sheng;
//	    }
//	    if(id!=null&&id.length()>0){
//	    	if(!machines.getDqM().equals(id)){
//	    		machines.setDqM(id);
//	    	}
//	    	
//	    }
		if(userMachineType != null){
			machines.setDqM(userMachineType.getDqM());
		}
		
	    //保存批次，默认为当前年月日
	    machines.setBatch(DateTimeUtils.formatDate(new Date(),"yyyyMMdd"));
	    machines.setCreateTime(new Date());
		machinesService.save(machines);
		List<MachineTypeNameValue> machineTypeNameValueList = machineTypeNameValueService.findByCondition(machines.getTheIdMachine());
		for(int i=0;i<machineTypeNameValueList.size();i++){
			MachineTypeNameValue machineTypeNameValue = machineTypeNameValueList.get(i);
			MachinesConf mc = new MachinesConf();
			mc.setMid(mid);
	    	mc.setConfName(machineTypeNameValue.getName());
	    	mc.setConfValue(machineTypeNameValue.getValue());
	    	
	    	machinesConfService.save(mc);
		}
		
		//保存已选择的应用
	    String rightList = getRequest().getParameter("selectRightList");
	    String asapply[] = rightList.split(",");
	    	if(asapply!=null&&asapply.length>0){
	    		MachinesApply mapply = new MachinesApply();
	    		for(int i=0;i<asapply.length;i++){
	    			mapply.setAsid(asapply[i]);//应用ID
	    			mapply.setMsid(mid);//机器ID
	    			mapply.setStatus(0);
	    			mapply.setSentTime(new Timestamp(System.currentTimeMillis()));
	    			mapply.setStartUserName(getUserAccount());
	    			//mapply.setEndUserName(asapply[i]);
	    			machinesApplyService.save(mapply);
	    		}
	    		
	    	}
	    	machines.setMid("");
	    	machines.setCompCode("");
	    	machines.setDeviceCode("");
	    	machines.setDeviceRandom("");
	    	
		return doFindById();
	}
	
	/**
	 * 删除终端信息
	 * @return
	 * @throws Exception
	 */
	public String doDelete() throws Exception{
		String machinesId = getRequest().getParameter("machinesId");
		Machines machines = machinesService.findById(machinesId);
		machines.setStatus(0);//0下线，1上线
		machinesService.update(machines);
//		List<MachinesConf> machinesConfList = machinesConfService.findByMachineId(machinesId);
//		for(int i = 0;i<machinesConfList.size() ; i++){
//			MachinesConf machinesConf = machinesConfService.findById(machinesConfList.get(i).getMcid());
//			machinesConfService.delete(machinesConf);
//		}
//		machinesService.delete(machines);
		return doList();
	}
	
	/**
	 * 根据终端的Id来判断更新终端还是添加保存终端
	 * @return
	 */
	public String doFindById()throws Exception{
		String mid = getRequest().getParameter("machinesId");
		Machines machines = new Machines();
		List<UserMachineType> list = doListMachineTypeName();
		Map request = (Map)ActionContext.getContext().get("request");
		request.put("list",list);
		
		//String userName = getUserAccount();
		//UserInfo userInfo =new UserInfo();
		//userInfo = userInfoService.findByUserAccount(userName);
		if(!StringUtils.isEmpty(mid)){//更新
			List<MachinesConf> machinesConfList = machinesConfService.findByMachineId(mid);//根据终端Id获取对应的终端配置信息
			 machines = machinesService.findById(mid);
			machinesModel.setMachines(machines);
			getRequest().setAttribute("machinesConfList", machinesConfList);
			//可选应用
			List<Object> resList = machinesApplyService.findByApplyByAudit(machines.getMid(), machines.getTheIdMachine(), getUserAccount());
			
			getRequest().setAttribute("resList", resList);
			//已选应用
			List<Object> asList = machinesApplyService.findByApplyByAsAudit(mid,machines.getTheIdMachine());
			getRequest().setAttribute("asList", asList);
			
			
			machines.setCompCode(getEfUserSession().getEfUser().getCompCode());
			
			return UPDATE;
		}else{
			
			machines.setDeviceRandom(IdentityGenerator.getInstance().getCharacterAndNumber(6));//随机生成6位的机器随机码
			machines.setCompCode(getEfUserSession().getEfUser().getCompCode());
			machinesModel.setMachines(machines);
			
			String theIdMachine = null;
			//根据审核条件查询出对应的machinesApply列表信息
			for(int i=0;i<list.size();i++){
				UserMachineType umt = (UserMachineType)list.get(0);
				//取出默认第一个
				theIdMachine = umt.getTheIdMachine();
				break;
			}
			//可选应用,这里的可选应用不用过滤已添加的应用
			List<Object> resList = machinesApplyService.findByApplyByAuditNew( theIdMachine, getUserAccount());
			
			getRequest().setAttribute("resList", resList);
			//已选应用,新增信息终端默认没有可选应用
			List<Object> asList = new ArrayList();
			getRequest().setAttribute("asList", asList);
			
			List<MachineTypeNameValue> machineTypeNameValueList = machineTypeNameValueService.findByCondition(theIdMachine);
			getRequest().setAttribute("machineTypeNameValueList", machineTypeNameValueList);
			
			
			return ADD;
		}
		
	}
	
	
	
	/**
	 * 更新终端信息
	 * @return
	 * @throws Exception
	 */
	public String doUpdate() throws Exception{
		
		Machines machines = machinesModel.getMachines();
		String theIdMachine = machines.getTheIdMachine();
		UserMachineType userMachineType = userMachineTypeService.findById(theIdMachine);
		PrintWriter out;
		getResponse().setCharacterEncoding("UTF-8");
		
		//组合键ID
		String machinesID = machines.getCompCode()+
							machines.getDeviceCode()+machines.getDeviceRandom();
		machines.setMachinesID(machinesID);
		
		//增加地区码
//		String sheng =	machines.getSelA();//省
//	    String shi =	machines.getSelB();//市
//	    String qu = machines.getSelC();//县/区
//	    String id = "";
//	    if(qu!=null&&qu.length()>0){
//	    	 id = qu;
//	    }else if(shi!=null&&shi.length()>0){
//	    	
//	    	 id = shi;	
//	    }else if(sheng!=null&&sheng.length()>0){
//	    	 id = sheng;
//	    }
//	    if(id!=null&&id.length()>0){
//	    	if(!machines.getDqM().equals(id)){
//	    		machines.setDqM(id);
//	    	}
//	    	
//	    }
		if(userMachineType != null){
			machines.setDqM(userMachineType.getDqM());
		}
		machines.setCreateTime(new Date());
		machinesService.update(machines);
		
		
		out = getResponse().getWriter();
		out.write("1");
	
		
		return null;
	}
	
	
	/**
	 * 根据machines查询设备类型
	 */
	 public List<Options> findByCode(){
		
		List<Options> list = this.machinesService.findByCode("machines");
		return list;
	}
	
	 /**
	  * 显示我的终端类型，下拉列表显示
	  * @return
	 * @throws IOException 
	  */
	 public List<UserMachineType> doListMachineTypeName() throws IOException{
		 String userName = getUserAccount();
		 List<UserMachineType> list = this.machinesService.doListMachineTypeName(userName);
		 return list;
	 }
	 
	 /**
	  * 根据我的设备类型查找对应的应用
	  */
	 public String doFindByOptionsId()throws Exception{
			String mid = getRequest().getParameter("machinesId");
			String optionsId = getRequest().getParameter("optionsId");
			
			Machines machines = machinesModel.getMachines();
			
			List<UserMachineType> list = doListMachineTypeName();
			Map request = (Map)ActionContext.getContext().get("request");
			request.put("list",list);
			String theIdMachine = optionsId;
		
			
			
			if(!StringUtils.isEmpty(mid)){//更新
				List<MachinesConf> machinesConfList = machinesConfService.findByMachineId(mid);//根据终端Id获取对应的终端配置信息
			    machines = machinesService.findById(mid);
				machinesModel.setMachines(machines);
				//我的设备类型通过手动选中的为默认值
				machines.setTheIdMachine(theIdMachine);
				getRequest().setAttribute("machinesConfList", machinesConfList);
				//getRequest().setAttribute("machines", machines);
				//可选应用
				List<Object> resList = machinesApplyService.findByApplyByAudit(machines.getMid(), theIdMachine, getUserAccount());
				
				getRequest().setAttribute("resList", resList);
				//已选应用
				List<Object> asList = machinesApplyService.findByApplyByAsAudit(mid,theIdMachine);
				getRequest().setAttribute("asList", asList);
				return UPDATE;
			}
			//可选应用新增时不过滤已选
			List<Object> resList = machinesApplyService.findByApplyByAuditNew( theIdMachine, getUserAccount());
			
			getRequest().setAttribute("resList", resList);
			//已选应用
			List<Object> asList = machinesApplyService.findByApplyByAsAudit(mid,theIdMachine);
			getRequest().setAttribute("asList", asList);
	    	
			List<MachineTypeNameValue> machineTypeNameValueList = machineTypeNameValueService.findByCondition(theIdMachine);
			getRequest().setAttribute("machineTypeNameValueList", machineTypeNameValueList);
			
			return ADD;
		}
	 
	 /**
	  * 增加前，校验机器终端是否已经存在
	  * @throws Exception
	  */
     	public void doCheckMachinesIsExist() throws Exception{
		 
     		String mid = getRequest().getParameter("mid");
     		 
		 String result = "true";//存在
		 String compCode = getRequest().getParameter("compCode");
		 String deviceCode = getRequest().getParameter("deviceCode");
		 String deviceRandom = getRequest().getParameter("deviceRandom");
		 
		 Machines machines = this.machinesService.findByCondition(compCode, deviceCode, deviceRandom);
		 if(machines == null || (mid!=null && mid.equals(machines.getMid()))){
			 result = "false";//不存在
		 }
		 getResponse().setCharacterEncoding("UTF-8");
			PrintWriter out = getResponse().getWriter();
			out.write(result);
	 }
     	
     	/**
    	 * 根据终端的Id来判断更新终端还是添加保存终端
    	 * @return
    	 */
    	public String doChangeById()throws Exception{
    		String mid = getRequest().getParameter("machinesId");
    		Machines machines = new Machines();
    		List<UserMachineType> list = doListMachineTypeName();
    		Map request = (Map)ActionContext.getContext().get("request");
    		request.put("list",list);
    		
    		String userName = getUserAccount();
    		
    		if(!StringUtils.isEmpty(mid)){//更新
    			List<MachinesConf> machinesConfList = machinesConfService.findByMachineId(mid);//根据终端Id获取对应的终端配置信息
    			 machines = machinesService.findById(mid);
    			machinesModel.setMachines(machines);
    			getRequest().setAttribute("machinesConfList", machinesConfList);
    			//可选应用
    			List<Object> resList = machinesApplyService.findByApplyByAudit(machines.getMid(), machines.getTheIdMachine(), getUserAccount());
    			
    			getRequest().setAttribute("resList", resList);
    			//已选应用
    			List<Object> asList = machinesApplyService.findByApplyByAsAudit(mid,machines.getTheIdMachine());
    			getRequest().setAttribute("asList", asList);
    			
    			
    			machines.setCompCode(getEfUserSession().getEfUser().getCompCode());
    			machines.setDeviceCode("");
    			//machines.setDeviceRandom("");
    			
    			
    			return "change";
    		}
    		return null;
    	}
    	
    	/**
    	 * 更换机器之后，相当于增加一个新机器，旧机器就下线
    	 * @return
    	 * @throws Exception
    	 */
    	public String doChangeSave() throws Exception{
    		Machines machines = machinesModel.getMachines();
    		String mid = IdentityGenerator.getInstance().getmacId();
    		String oldMid = getRequest().getParameter("oldMid");
    		
    		machines.setStatus(1);//新机器上线,1上线，0下线
    		machines.setMid(mid);
    		machines.setUserName(getUserAccount());
    		
    		//组合键ID
    		String machinesID = machines.getCompCode()+
    							machines.getDeviceCode()+machines.getDeviceRandom();
    		machines.setMachinesID(machinesID);
    		machines.setBatch(machines.getBatch());
    		//type暂时不使用，随便保存一个默认值
    		machines.setType("0000000000000000");
    		machines.setCreateTime(new Date());
    		//保存终端信息
    		machinesService.save(machines);
    		//保存自定义值
    		List<MachinesConf> machinesConfList = machinesConfService.findByMachineId(mid);//根据终端Id获取对应的终端配置信息
    		for(int i=0;i<machinesConfList.size();i++){
    			MachinesConf mcold = machinesConfList.get(i);
    			MachinesConf mc = new MachinesConf();
    			mc.setMid(mid);
		    	mc.setConfName(mcold.getConfName());
		    	mc.setConfValue(mcold.getConfValue());
		    	machinesConfService.save(mc);
    		}
    		 
    		//保存已选择的应用
    		//已选应用
			List<Object> asList = machinesApplyService.findByApplyByAsAudit(mid,machines.getTheIdMachine());
			for(int i=0;i<asList.size();i++){
				AppleServer appServerold = (AppleServer)asList.get(i);
				MachinesApply mapply = new MachinesApply();
				mapply.setAsid(appServerold.getAsid());//应用ID
    			mapply.setMsid(mid);//机器ID
    			mapply.setStatus(0);
    			mapply.setSentTime(new Timestamp(System.currentTimeMillis()));
    			mapply.setStartUserName(getUserAccount());
    			
    			machinesApplyService.save(mapply);
			}
    	    
			Machines oldMachines = machinesService.findById(oldMid);
	    	oldMachines.setStatus(0);//旧机器更换后下线，0下线，1上线
	    	oldMachines.setCreateTime(new Date());
    		machinesService.update(oldMachines);	
    	    	
    		return doList();
    	}
	
    	/**
   	  * 更换前，校验机器终端是否已经存在
   	  * @throws Exception
   	  */
        	public void doChangeMachinesIsExist() throws Exception{
   		 
        		 
   		 String result = "true";//存在
   		 String compCode = getRequest().getParameter("compCode");
   		 String deviceCode = getRequest().getParameter("deviceCode");
   		 String deviceRandom = getRequest().getParameter("deviceRandom");
   		 
   		 Machines machines = this.machinesService.findByCondition(compCode, deviceCode, deviceRandom);
   		 if(machines == null){
   			 result = "false";//不存在
   		 }
   		 getResponse().setCharacterEncoding("UTF-8");
   			PrintWriter out = getResponse().getWriter();
   			out.write(result);
   	 }
    	
        	/**
        	 * 下线前先判断还有没关联
        	 * @throws IOException
        	 */
        	public void checkIsOffLine() throws IOException{
        		String mid = getRequest().getParameter("machinesId");
        		String flag = "0";//0可以下线
        			flag = machinesApplyService.findByMsid(mid);//0可以下线，大于0不可以下线
        			 getResponse().setCharacterEncoding("UTF-8");
        	   			PrintWriter out = getResponse().getWriter();
        	   			out.write(flag);
        	}
        	
        	/**
        	 * 跳转到导入信息终端
        	 * @return
        	 * @throws Exception
        	 */
        	public String toImport() throws Exception{
//        		getRequest().setAttribute("methodName", "importMachines");
//        		Machines machines = machinesModel.getMachines();
        		return TOIMPORT;
        	}
        	
        	public void importMachines() throws Exception{
        		HttpServletResponse response =  getResponse();
        		JSONObject json=new JSONObject();
        	   
        		String userName = getUserAccount();//导入用户名
        		String compCode = getEfUserSession().getEfUser().getCompCode();//厂家编码
        		
//        		PrintWriter out = getResponse().getWriter();
        		try
        		{
        			response.setContentType("text/html;charset=UTF-8");
	        		if(fileUpload == null)
	    			{
	    				log.error("上传Excel文件为空");
	    				json.put("result", "1");
	        			JSONResponseHepler.writeJson(response, json);
//	    				out.write("上传Excel文件为空");
	        			return;
	    			}
	        		ExcelFileFilter excelFileFilter = new ExcelFileFilter();
	    			excelFileFilter.getSheet(new FileInputStream(fileUpload));
	    			
	    			if(excelFileFilter.getRows() > 5000) {
	    				log.error("上传Excel文件不能大于5000行");
	    				json.put("result", "2");
	        			JSONResponseHepler.writeJson(response, json);
	        			return;
	    			}
	    			
	    			if(excelFileFilter.getCols() != 2) {
	    				log.error("上传Excel文件只能为二列");
	    				json.put("result", "3");
	        			JSONResponseHepler.writeJson(response, json);
//	    				out.write("上传Excel文件只能为二列");
	        			return;
	    			}
	    			
	    			List<String[]> list = excelFileFilter.filter();
	    			ResultUtil resultUtil = machinesService.getListMachines(compCode, userName, list);
//	    			List<Machines> machinesList = machinesService.getListMachines(compCode, userName, list);
	    			String status = resultUtil.getStatus();
	    			String result = resultUtil.getResult();
	    			if(status.equals("1")){
	    				log.error(result);//信息终端类型不存在，请修改后得新导入！
	        			json.put("result", "4:"+result);
	        			JSONResponseHepler.writeJson(response, json);
	        			return;
	    			}else if(status.equals("2")){
	    				log.error(result);//信息终端编号 "+deviceCode+" 存在,请检查！
	        			json.put("result", "5:"+result);
	        			JSONResponseHepler.writeJson(response, json);
	        			return;
	    			}else if(status.equals("0")){
	    				List<Machines> machinesList = resultUtil.getMachinesList();
	    				machinesService.insertBatch(machinesList);
	        			json.put("result", "0");
	        			JSONResponseHepler.writeJson(response, json);
	    			}
	    			
        	}catch (Exception e)
    		{
    			log.error("上传文件出错，" + e.toString());
    			e.printStackTrace();
    			json.put("result", "6");
    			JSONResponseHepler.writeJson(response, json);
    		}
        	
        } 	
        	//跳到批号列表，根据批号导出信息终端
        	public String toExport() throws Exception{
        		//列出信息终端的所有批次，批次格式：20120426
        		List<Object> resList = machinesService.findByBatch();
        		getRequest().setAttribute("resList", resList);
        		return TOEXPORT;
        	}
        	//
        	public String exportMachines() throws Exception{
        		HttpServletResponse response =  getResponse();
        		JSONObject json=new JSONObject();
        	    
        	    response.setContentType("application/vnd.ms-excel; charset=utf-8");
        	    response.setHeader("Content-disposition", "attachment; filename=machinesBatch.xls");
        	    
        	    jxl.write.WritableWorkbook wwb;
        	    String path = getRequest().getRealPath("");
        	   
	       	    wwb = Workbook.createWorkbook(new File( path+File.separator+"machinesBatch"+getUserAccount()+".xls"));
	       	    jxl.write.WritableSheet ws = wwb.createSheet("信息终端", 0);
	       		// 生成文件表头				
	       		ws.addCell( new jxl.write.Label(0, 0 , "厂家编码" ) );
	       		ws.addCell( new jxl.write.Label(1, 0 , "机器码" ) );
	       		ws.addCell( new jxl.write.Label(2, 0 , "随机码" ) );
	       		
        		try
        		{
        			String batch = getRequest().getParameter("batch");
        			machinesModel.setBatch(batch);
        			machinesModel.setUserName(getUserAccount());
        			List<Machines> maList = machinesService.findByCondition(machinesModel);
        			for(int i=0;i<maList.size();i++){
        				Machines mac  = maList.get(i);
        				ws.addCell( new jxl.write.Label(0, i + 1 , mac.getCompCode()) );
        				ws.addCell( new jxl.write.Label(1, i + 1 , mac.getDeviceCode()) );
        				ws.addCell( new jxl.write.Label(2, i + 1 , mac.getDeviceRandom()) );
        			}
        			
        			wwb.write();							
        			
        			wwb.close();
        			json.put("result", "1");
        			json.put("filename", "machinesBatch"+getUserAccount()+".xls");
        			JSONResponseHepler.writeJson(response, json);
        	}catch (Exception e)
    		{
    			log.error("批量导出文件出错，" + e.toString());
    			e.printStackTrace();
    			json.put("result", "6");
    			JSONResponseHepler.writeJson(response, json);
    		}
        	return null;
        	
        } 
}
